<?php

namespace app\admin\controller\coupon;

use app\common\controller\Backend;
use app\common\library\Auth;

/**
 * 优惠码使用记录
 *
 * @icon fa fa-user
 */
class Couponuse extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,coupon,coupon_id';

    /**
     * @var \app\admin\model\CouponList
     */
    protected $model = null;
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('CouponList');
    
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where(array('status' => array('NEQ', 3)))
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {

                //类型
                $list[$k]['coupon_number'] = $v['coupon'];

                //类型
                $list[$k]['type'] = "活动优惠券";
                //状态
                if($v['status'] == 0){
                    $list[$k]['status_title'] = "未使用";
                }else if($v['status'] == 1){
                    $list[$k]['status_title'] = "已使用";
                }else if($v['status'] == 2){
                    $list[$k]['status_title'] = "已过期";
                }else if($v['status'] == 3){
                    $list[$k]['status_title'] = "已删除";
                }

                //使用者
                $list[$k]['user_info'] = "-";
                //订单信息
                $list[$k]['order_info'] = "-";
                //优惠内容
                $list[$k]['coupon_info'] = "抵价".$v['money']."元";

                //批次名称
                $row = model('Coupon')->get($v['coupon_id'])->toArray();
                $list[$k]['title_info'] = $row['title'];

                //领取时间
                $list[$k]['start_time'] = "-";
                //使用时间
                $list[$k]['use_time'] = "-";

            }

    

            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

}
